package com.chen.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.domain.CoinRecharge;
import com.chen.mapper.CoinRechargeMapper;
import com.chen.service.CoinRechargeService;
import com.chen.service.RecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

@Service
public class CoinRechargeServiceImpl extends ServiceImpl<CoinRechargeMapper, CoinRecharge> implements CoinRechargeService{


    @Autowired
    private RecordsService<CoinRecharge> recordsService;

    @Resource
    private CoinRechargeMapper coinRechargeMapper;

    @Override
    public Page<CoinRecharge> findByPage(Page<CoinRecharge> page, Long coinId, Long userId, String userName, String mobile, Byte status, String numMin, String numMax, String startTime, String endTime) {
        try {


            RecordsServiceImpl.SearchRecords searchRecords = new RecordsServiceImpl.SearchRecords();

            searchRecords.setCoinId(coinId);
            searchRecords.setUserId(userId);
            searchRecords.setUserName(userName);
            searchRecords.setMobile(mobile);

            Map<RecordsServiceImpl.SearchRecords.BetweenDataEnum, Map<String, RecordsServiceImpl.SearchRecords.LimitRecords>> betweenData = new HashMap<>();

            //设置金钱区间
            Map<String, RecordsServiceImpl.SearchRecords.LimitRecords> limitRecordsMap = new HashMap<>();
            limitRecordsMap.put("amount", RecordsServiceImpl.SearchRecords.LimitRecords.builder().min(numMin).max(numMax).build());
            betweenData.put(RecordsServiceImpl.SearchRecords.BetweenDataEnum.AMOUNT, limitRecordsMap);

            //设置金钱区间
            limitRecordsMap = new HashMap<>();
            limitRecordsMap.put("created", RecordsServiceImpl.SearchRecords.LimitRecords.builder().min(startTime).max(endTime).build());
            betweenData.put(RecordsServiceImpl.SearchRecords.BetweenDataEnum.TIME, limitRecordsMap);


            searchRecords.setBetweenData(betweenData);

            return recordsService.findData(page,coinRechargeMapper,searchRecords);
        } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException | NoSuchFieldException e) {
            e.printStackTrace();
        }
        return null;
    }
}
